package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.ChannelQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Slf4j
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public PageResult<AdChannel> findByPage(ChannelQueryDTO dto) {
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        // 查询条件
        queryWrapper.like(!StringUtils.isEmpty(dto.getName()), AdChannel::getName, dto.getName());
        // 分页条件
        Page<AdChannel> ipage = new Page<AdChannel>(dto.getPage(), dto.getSize());
        // 查询
        Page<AdChannel> page = this.page(ipage, queryWrapper);
        if(CollectionUtils.isEmpty(page.getRecords())){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult<>(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }
}
